package com.cgnpc.scp.common.bean;

import org.springframework.core.io.InputStreamResource;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 定制的CustomDownloadFileInputStreamResource<br>
 * 注意为了下载大文件，很多数据可能不正确，请直接使用getFile()<br>
 */
public class CustomDownloadFileInputStreamResource extends InputStreamResource {
    private File localTempFile;

    public CustomDownloadFileInputStreamResource(InputStream inputStream) {
        super(inputStream);
    }

    public CustomDownloadFileInputStreamResource(InputStream inputStream, String description) {
        super(inputStream, description);
    }

    public CustomDownloadFileInputStreamResource(
            InputStream inputStream
            , String description
            , File file
    ) {
        super(inputStream, description);
        this.localTempFile = file;
    }

    @Override
    public File getFile() throws IOException {
        // return super.getFile();
        return this.localTempFile;
    }

    @Override
    public String getFilename() {
        // return super.getFilename();
        return this.localTempFile == null ? null : this.localTempFile.getName();
    }

    /**
     * @return
     * @throws IOException
     * @throws IllegalStateException
     * @see #getFile()
     */
    @Deprecated
    @Override
    public InputStream getInputStream() throws IOException, IllegalStateException {
        return super.getInputStream();
    }
}
